// pages/order/myorder/myorder.js

const app = getApp()
var util = require('../../../utils/util.js')
var http = require('../../../utils/server.js')

Page({
  data: {
    current: 0,
    keyWords: null,
    footer: {
      index: 1
    },
    datas: [],
    pages: [0, 0, 0, 0],
    noMore: [false, false, false, false]
  },
  onLoad: function(options) {
    var user = wx.getStorageSync("userInfo");
    if (user.gangId == "")
      wx.hideTabBar()
    else
      wx.showTabBar()
    this.setData({
      userInfo: user
    })
  },
  onShow: function() {
    this.loadOrderList(this.data.current, this.getStatus(this.data.current), 1);
  },
  onPullDownRefresh: function () {
    this.onShow()
  },

  onReachBottom: function(e) {
    var index = this.data.current;
    var page = this.data.pages[index] + 1;
    this.loadOrderList(index, this.getStatus(index), page, true);
  },
  input: function(e) {
    console.info(e);
    this.data.keyWords = e.detail.value;
  },
  search: function() {
    this.loadOrderList(this.data.current, this.getStatus(this.data.current), 1, true);
  },
  loadOrderList(index, status, page, refresh) {
    console.info(status);
    var that = this;
    if (page == 1) {
      that.data.noMore[index] = false;
    }
    if (that.data.noMore[index]) {
      return;
    }
    that.data.pages[index] = page;
    //商品列表
    http.post({
      url: "Order/getList",
      keyword: that.data.keyWords == null ? "" : that.data.keyWords,
      status: status,
      page: page
    }, function(data) {
      var datas = data.data;
      if (that.data.userInfo.gangRole != data.gang_role) {
        wx.setStorageSync('userInfo', null); 
        wx.reLaunch({ url: '/pages/index/welcome'})
      }
      if (page == 1) {
        that.data.datas[index] = datas;
      } else {
        that.data.datas[index] = that.data.datas[index].concat(datas);
      }

      that.setData({
        datas: that.data.datas
      });
      console.log(that.data.datas);
      if (refresh) {
        wx.stopPullDownRefresh();
      }
      if (datas.length < 10) {
        that.data.noMore[index] = true;
        that.setData({
          noMore: that.data.noMore
        });
      }
    });
  },
  fahuo: function(e) {
    console.info(e);
    wx.navigateTo({
      url: '/pages/order/delivery/delivery?id=' + e.currentTarget.dataset.id,
    })
  },
  pay: function(e) {
    http.post({
      url: "order/pay",
      order_id: e.currentTarget.dataset.id,
    }, function(data) {
      wx.requestPayment({
        'timeStamp': data.timeStamp,
        'nonceStr': data.nonceStr,
        'package': data.package,
        'signType': data.signType,
        'paySign': data.paySign,
        'success': function(e) {
          console.log("支付成功！", e)
          util.showSuccess('支付成功！', function() {
            wx.redirectTo({
              url: "/pages/order/paySuccess/paySuccess?order_id=" + data.order_id + '&money=' + data.recharge
            })
          })
        },
        'fail': function(e) {
          console.log("支付失败！", e)
          util.showError('支付失败！')
        }
      })
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  details: function(e) {
    console.info(e);
    wx.navigateTo({
      url: '/pages/order/orderdetails/orderdetails?id=' + e.currentTarget.dataset.id,
    })
  },
  wuliu: function(e) {
    wx.navigateTo({
      url: '/pages/order/logis/logistical?order_id=' + e.currentTarget.dataset.id,
    })
  },
  change: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.setData({
      current: index
    });
    // if (that.data.pages[index] > 0) {
    //   return;
    // }
    that.loadOrderList(index, that.getStatus(index), 1);

  },
  getStatus: function(index) {
    var status = "0,1";
    switch (parseInt(index)) {
      case 0:
        status = "0,1";
        break;
      case 1:
        status = "2";
        break;
      case 2:
        status = "9,10,11,12";
        break;
      case 3:
        status = "4,5,6,7,8,13,14,15";
        break;
    }
    return status;
  },
  receive: function(e) {
    var that = this;
    var order_id = e.currentTarget.dataset.id
    var index = e.currentTarget.dataset.index
    wx.showModal({
      title: '操作提示',
      content: '确定收货吗?',
      success: function(res) {
        if (res.confirm) {
          http.post({
            url: "Order/confirmReceipt",
            order_id: order_id
          }, function(data) {
            util.showSuccess("操作成功", function() {
              if (that.data.datas[that.data.current][index].product.length == 1) {
                wx.navigateTo({
                  url: '/pages/evaluate/evaluate/evaluate?order_id=' + order_id
                })
              } else
                wx.navigateTo({
                  url: '/pages/order/orderdetails/orderdetails?id=' + e.currentTarget.dataset.id,
                })
            })
          })
        }
      }
    })
  },
  evaluate: function(e) {
    var that = this;
    var order_id = e.currentTarget.dataset.id
    var index = e.currentTarget.dataset.index
    console.log(that.data.datas[that.data.current][index].product.length)

    if (that.data.datas[that.data.current][index].product.length == 1) {
      wx.navigateTo({
        url: '/pages/evaluate/evaluate/evaluate?order_id=' + order_id
      })
    } else
      wx.navigateTo({
        url: '/pages/order/orderdetails/orderdetails?id=' + e.currentTarget.dataset.id,
      })
  }
})